/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "rs_render_single_process_manager.h"

#include "rs_render_pipeline.h"
#include "render_process/transaction/rs_service_to_render_connection.h"
#include "rs_profiler.h"
#include "rs_render_service.h"
#include "rs_trace.h"

#include "rs_composer_connection.h"
#include "rs_render_composer_agent.h"
#include "rs_render_composer_manager.h"
#include "pipeline/main_thread/rs_main_thread.h"
#include "screen_manager/screen_types.h"
#include "transaction/rs_client_to_render_connection.h"

#undef LOG_TAG
#define LOG_TAG "RSSingleRenderProcessManager"

namespace OHOS {
namespace Rosen {
RSSingleRenderProcessManager::RSSingleRenderProcessManager(RSRenderService& renderService) :
    RSRenderProcessManager(renderService)
{
    // 单进程vsync创建
    sptr<VSyncIConnectionToken> vsyncToken = new IRemoteStub<VSyncIConnectionToken>();
    sptr<VSyncConnection> conn = new VSyncConnection(renderService.rsVSyncDistributor_, "rs", vsyncToken->AsObject());
    // conn->id_ = hgmConext_.
    renderService.rsVSyncDistributor_->AddConnection(conn);
    auto receiver = std::make_shared<VSyncReceiver>(conn, vsyncToken->AsObject(), renderService.handler_, "rs");
    receiver->Init();
    renderService.renderPipeline_ = RSRenderPipeline::Create(renderService.handler_, receiver);
    sptr<RSRenderServiceAgent> renderServiceAgent = sptr<RSRenderServiceAgent>::MakeSptr(renderService);
    sptr<RSRenderPipelineAgent> renderPipelineAgent = new RSRenderPipeline(renderService.renderPipeline_);
    serviceToRenderConnection_ = sptr<RSServiceToRenderConnection>::MakeSptr(renderServiceAgent, renderPipelineAgent);
    auto token = new IRemoteStub<RSIConnectionToken>();
    auto tokenObj = token->AsObject();
    renderPipelineAgent = new RSRenderPipelineAgent(renderService_.renderPipeline_);
    sptr<RSIClientToRenderConnection> newRenderConn(
        new RSClientToRenderConnection(renderService_.GetCallingPid(), &renderService_,
            renderService_.mainThread_, renderPipelineAgent, renderService_.screenManager_, tokenObj));
    clientToRenderConnection_ = newRenderConn;
}

sptr<IRemoteObject> RSSingleRenderProcessManager::OnScreenConnected(ScreenId id, const ScreenEventData& data, const RSScreenProperty& property)
{
    RSRenderProcessManager::OnScreenConnected(screenId, data, property);

    auto composerConn = renderService_.rsRenderComposerManager_->GetRSComposerConnection(property.GetScreenId());
    renderService_.rsRenderComposerManager_->SetServiceToRenderConnection(screenId, serviceToRenderConnection_);
    std::shared_ptr<RSRenderComposerClient> composerClient = RSRenderComposerClient::Create(false, composerConn);
    renderService_.renderPipeline_->OnScreenConnected(sptr<RSScreenProperty>::MakeSptr(property), composerClient);
    return clientToRenderConnection_->AsObject();
}

void RSSingleRenderProcessManager::OnScreenDisconnected(ScreenId id, const ScreenEventData& data)
{
    RSRenderProcessManager::OnScreenDisconnected(id, data);

    renderService_.renderPipeline_->OnScreenDisconnected(id);
}

void RSSingleRenderProcessManager::OnScreenPropertyChanged(ScreenId id, const RSScreenProperty& property)
{
    RSRenderProcessManager::OnScreenPropertyChanged(id, property);

    renderService_.renderPipeline_->OnScreenPropertyChanged(sptr<RSScreenProperty>::MakeSptr(property));
}

void RSSingleRenderProcessManager::OnFrameRefresh()
{
    // auto mainThread = RSMainThread::Instance();
    // if (mainThread == nullptr) {
    //     RS_LOGE("%{public}s: mainThread is nullptr", __func__);
    //     return;
    // }
    // mainThread->PostTask([mainThread]() {
    //     mainThread->SetForceUpdateUniRenderFlag(true);
    //     mainThread->RequestNextVSync();
    // });
    return;
}

void RSSingleRenderProcessManager::OnVBlankIdle(ScreenId id, uint64_t ns)
{
    // RSHardwareThread::Instance().PostTask([screenId, ns]() {
    //     RSHardwareThread::Instance().OnScreenVBlankIdleCallback(screenId, ns);
    // });
    return;
}

void RSSingleRenderProcessManager::OnVirtualScreenConnected(ScreenId id)
{
    return;
}

void RSSingleRenderProcessManager::OnVirtualScreenDisconnected(ScreenId id)
{
    return;
}

sptr<RSIServiceToRenderConnection> RSSingleRenderProcessManager::GetServiceToRenderConn(ScreenId screenId)
{
    return serviceToRenderConnection_;
}

std::vector<sptr<RSIServiceToRenderConnection>> RSSingleRenderProcessManager::GetServiceToRenderConns()
{
    return { serviceToRenderConnection_ };
}

sptr<RSIClientToRenderConnection> RSSingleRenderProcessManager::GetClientToRenderConn(ScreenId screenId)
{
    return clientToRenderConnection_;
}
} // namespace Rosen
} // namespace OHOS